//
//  XGL_CommentInputBar.swift
//  XGamesLive
//
//  Created by ios on 2024/5/22.
//

import UIKit

class XGL_CommentInputBar: UIView {

    var contentView: UIView!
    var inputTextView: PanTextView!
    var giftItem: UIButton!
    var sendItem: UIButton!
    private var textOriginalHeight: CGFloat = 36.0
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.hexColor("#001310").withAlphaComponent(0.5)
        self.createXglView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.layoutIfNeeded()
        self.contentView.addCorner(conrners: [.topLeft,.topRight], radius: 24.0)
    }
}

extension XGL_CommentInputBar{
   
    private func handleTextHeight(textView: UITextView) {

        let size = textView.sizeThatFits(CGSize(width: textView.bounds.width, height: CGFloat(MAXFLOAT)))
        let currentHeight = CGFloat(ceilf(Float(size.height))) > self.textOriginalHeight ? CGFloat(ceilf(Float(size.height))) : self.textOriginalHeight
        if currentHeight > self.textOriginalHeight{
            self.inputTextView.layerCornerRadius = 4.0
        }else{
            self.inputTextView.layerCornerRadius = self.textOriginalHeight / 2.0
        }
        self.inputTextView.snp.updateConstraints { make in
            make.height.equalTo(currentHeight)
        }
        UIView.animate(withDuration: 0.25) {
            self.superview!.layoutIfNeeded()
        }
    }
}

extension XGL_CommentInputBar: UITextViewDelegate{
    func textViewDidChange(_ textView: UITextView) {

        self.handleTextHeight(textView: textView)
        
    }
}

extension XGL_CommentInputBar{
    private func createXglView(){
        self.contentView = UIView().then({ make in
            make.backgroundColor = UIColor.hexColor("#001310")
        })
        let bottomView = UIView().then { make in
            make.backgroundColor = UIColor.hexColor("#001310")
        }
        self.inputTextView = PanTextView().then({ make in
            make.textColor = UIColor.white
            make.placeholder = "Say something"
            make.placeholderColor = UIColor.hexColor("#A2A2A2")
            make.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
            make.backgroundColor = .clear
            make.layerCornerRadius = 18.0
            make.clipsToBounds = true
            make.delegate = self
        })
        self.giftItem = UIButton().then({ make in
            make.setImage(imageWithName("icon_xgl_input_bar_gift"), for: .normal)
            
        })
        self.sendItem = UIButton().then({ make in
            make.setImage(imageWithName("icon_xgl_input_bar_send"), for: .normal)
        })
        self.contentView.addSubviews([self.inputTextView,self.giftItem,self.sendItem])
        self.addSubviews([self.contentView,bottomView])
        self.inputTextView.snp.makeConstraints { make in
            make.top.equalTo(10.0)
            make.left.equalTo(30.0)
            make.right.equalTo(giftItem.snp.left).offset(-16.0)
            make.height.equalTo(36.0)
        }
        self.giftItem.snp.makeConstraints { make in
            make.top.equalTo(6.0)
            make.right.equalTo(sendItem.snp.left).offset(-16.0)
            make.width.height.equalTo(36.0)
        }
        self.sendItem.snp.makeConstraints { make in
            make.top.equalTo(6.0)
            make.right.equalTo(-15.0)
            make.width.equalTo(76.0)
            make.height.equalTo(36.0)
        }
        self.contentView.snp.makeConstraints { make in
            make.width.centerX.equalToSuperview()
            make.bottom.equalTo(bottomView.snp.top)
            make.bottom.equalTo(self.inputTextView.snp.bottom).offset(6.0)
        }
        bottomView.snp.makeConstraints { make in
            make.width.centerX.bottom.equalToSuperview()
//            make.top.equalTo(self.contentView.snp.bottom)
            make.height.equalTo(BottomHeight)
        }
        self.snp.makeConstraints { make in
            make.top.equalTo(self.contentView.snp.top)
        }
    }
}
